﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MarchEstate.Manager.BusinesEntity.Costcalculate
{
    public class DateHelper
    {
        /// <summary>
        /// 按月分段
        /// 比如：2012-5-12 到 2012-7-12
        /// 此函数把它按月分成两段 2012-5-12 到2012-6-12 、2012-6-12 到2012-7-12
        /// </summary>
        /// <param name="bdate"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public static List<DateTime[]> GetDateSegment(DateTime bdate, DateTime edate)
        {
            List<DateTime[]> DateSegmentList = new List<DateTime[]>();

            //开始时间大于等于结束时间的话 肯定返回的就是空的时间段
            if (bdate >= edate) { return DateSegmentList; }

            DateTime bsegment, esegment;

            //DateTime spotdate = bdate.AddDays(1);//从上次到期日的后一天开始
            DateTime spotdate = bdate;
            while (true)
            {
                bsegment = spotdate;
                spotdate = spotdate.AddMonths(1);//累加1一个月  因为是以月为时间段
                //如果累加一个月后的时间比原始结束时间的大或等于 则表示这到了最后一个时间段，且到期日为原始结束时间
                if (spotdate >= edate)//edate.AddDays(1)
                {
                    esegment = edate;
                    DateSegmentList.Add(new DateTime[] { bsegment, esegment });
                    break;
                }
                else
                {
                    //esegment = spotdate.AddDays(-1);
                    esegment = spotdate;
                    DateSegmentList.Add(new DateTime[] { bsegment, esegment });
                }
            }

            return DateSegmentList;
        }

        /// <summary>
        /// 一月段如果跨月分成两段
        /// </summary>
        /// <param name="bsegment"></param>
        /// <param name="esegment"></param>
        /// <returns></returns>
        public static List<DateTime[]> GetDaySegment(DateTime bsegment, DateTime esegment)
        {
            List<DateTime[]> daysegList = new List<DateTime[]>();
            DateTime bdayseg, edayseg;

            if (esegment.Month == bsegment.Month)
            {
                bdayseg = bsegment;
                edayseg = esegment;
                daysegList.Add(new DateTime[] { bdayseg, edayseg });
            }
            else
            {
                bdayseg = bsegment;
                edayseg = bdayseg;
                while (true)
                {
                    edayseg = edayseg.AddDays(1);
                    if (edayseg.Month != bdayseg.Month)
                    {
                        edayseg = edayseg.AddDays(-1);
                        break;
                    }
                }
                daysegList.Add(new DateTime[] { bdayseg, edayseg });
                bdayseg = edayseg.AddDays(1);
                edayseg = esegment;
                daysegList.Add(new DateTime[] { bdayseg, edayseg });
            }

            return daysegList;
        }
        /// <summary>
        /// 得到指定年月的当月天数
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public static int GetMonthDayNum(int year, int month)
        {
            DateTime bdate = new DateTime(year, month, 1);
            DateTime edate = bdate.AddMonths(1);
            return ((TimeSpan)(edate - bdate)).Days;
        }

        /// <summary>
        /// 得到两个时间之间的天数
        /// </summary>
        /// <param name="bdayseg"></param>
        /// <param name="edayseg"></param>
        /// <returns></returns>
        public static int GetDays(DateTime bdayseg, DateTime edayseg)
        {
            return ((TimeSpan)(bdayseg - edayseg)).Days + 1;
        }
    }
}
